home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Magazine / C_Tutorial / Part-6 / asl3 / idcmp.c < prev    next >
C/C++ Source or Header  |  1997-10-27  |  5KB  |  201 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7.  
  8. #include<string.h>
  9.  
  10. #include<clib/exec_protos.h>
  11. #include<clib/gadtools_protos.h>
  12. #include<clib/graphics_protos.h>
  13. #include<clib/intuition_protos.h>
  14.  
  15. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  16. static int  doMenuPick(struct Window*, UWORD);
  17.  
  18. /* Our message handling code */
  19. void handleIDCMP()
  20. {
  21.     char* text = "Hello World!";
  22.     int going = TRUE;
  23.     int drawing = FALSE;
  24.     ULONG drawsig, toolsig, gotsig;
  25.     struct Window* drawwin = getDrawWin();
  26.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  27.     while(going)
  28.     {
  29.         struct IntuiMessage* intuimsg;
  30.         /* Only include tool window signal mask if window is open */
  31.         toolsig = getToolSig();
  32.         /* Wait for messages to arrive */
  33.         gotsig = Wait(drawsig | toolsig);
  34.         /* Messages have arrived: loop through all of them */
  35.         /* Check messages from the drawing window first */
  36.         if(gotsig & drawsig)
  37.         {
  38.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  39.             {
  40.                 /* Copy the important bits of the message */
  41.                 ULONG class = intuimsg->Class;
  42.                 UWORD code = intuimsg->Code;
  43.                 WORD mousex = intuimsg->MouseX;
  44.                 WORD mousey = intuimsg->MouseY;
  45.                 /* Reply when finished copying bits from message */
  46.                 GT_ReplyIMsg(intuimsg);
  47.                 /* Act on this message... */
  48.                 switch(class)
  49.                 {
  50.                 case IDCMP_MOUSEBUTTONS:
  51.                     switch(code)
  52.                     {
  53.                     case SELECTDOWN:
  54.                         drawing = TRUE;
  55.                         break;
  56.                     case SELECTUP:
  57.                         drawing = FALSE;
  58.                         break;
  59.                     }
  60.                     /* break; omitted so we draw on click, too */
  61.                 case IDCMP_MOUSEMOVE:
  62.                     if(drawing)
  63.                     {
  64.                         Move(drawwin->RPort, mousex, mousey);
  65.                         Text(drawwin->RPort, text, strlen(text));
  66.                     }
  67.                     break;
  68.                 case IDCMP_MENUPICK:
  69.                     going = doMenuPick(drawwin, code);
  70.                     drawwin = getDrawWin();
  71.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.         /* Now check messages from the tool window */
  77.         if(going && (gotsig & toolsig))
  78.         {
  79.             struct Window* toolwin = getToolWin();
  80.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  81.             {
  82.                 /* Copy the important bits of the message */
  83.                 ULONG class = intuimsg->Class;
  84.                 UWORD code = intuimsg->Code;
  85.                 APTR iaddr = intuimsg->IAddress;
  86.                 /* Reply when finished copying bits from message */
  87.                 GT_ReplyIMsg(intuimsg);
  88.                 /* Act on this message... */
  89.                 switch(class)
  90.                 {
  91.                 case IDCMP_CLOSEWINDOW:
  92.                     closeToolWin();
  93.                     /* Update our local toolwin, so we stop loop */
  94.                     toolwin = NULL;
  95.                     uncheckToolBar(drawwin);
  96.                     break;
  97.                 case IDCMP_REFRESHWINDOW:
  98.                     /* You *MUST* remember to ask for and handle these refresh messages */
  99.                     GT_BeginRefresh(toolwin);
  100.                     GT_EndRefresh(toolwin, TRUE);
  101.                     break;
  102.                 case IDCMP_GADGETUP:
  103.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  104.                     break;
  105.                 case IDCMP_MENUPICK:
  106.                     going = doMenuPick(drawwin, code);
  107.                     /* Update our local toolwin, so we stop loop */
  108.                     toolwin = getToolWin();
  109.                     drawwin = getDrawWin();
  110.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. /* Process IDCMP_GADGETUP event */
  119. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  120. {
  121.     switch(gad->GadgetID)
  122.     {
  123.     case MYBUT_ID:
  124.         /* Our button was clicked!  Set foreground to next pen colour */
  125.         nextFgPen(drawwin);
  126.         break;
  127.     case MYPAL_ID:
  128.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  129.         setFgPen(drawwin, code);
  130.         break;
  131.     }
  132. }
  133.  
  134. /* Process IDCMP_MENUPICK event */
  135. static int doMenuPick(struct Window* drawwin, UWORD code)
  136. {
  137.     UWORD menuCode, menuNumber, itemNumber;
  138.     /* Loop over all the menu selections in the menu code */
  139.     struct MenuItem* item;
  140.     for(menuCode = code;
  141.             menuCode != MENUNULL;
  142.             menuCode = item->NextSelect)
  143.     {
  144.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  145.         /* Extract the menu number and menu item number from the menu code */
  146.         menuNumber = MENUNUM(menuCode);
  147.         itemNumber = ITEMNUM(menuCode);
  148.         /* Now decide what to do based on what menu item was selected */
  149.         switch(menuNumber)
  150.         {
  151.         case 0:  /* Project menu */
  152.             /* Only one item: Quit */
  153.             switch(itemNumber)
  154.             {
  155.             case 0:  /* Load */
  156.                 return load();
  157.             case 1:  /* Save */
  158.                 save();
  159.                 break;
  160.             case 3:  /* Quit (item 2 is the bar!) */
  161.                 return FALSE;
  162.             }
  163.             break;
  164.         case 1:  /* Pen menu */
  165.             switch(itemNumber)
  166.             {
  167.             case 0:  /* Next */
  168.                 nextFgPen(drawwin);
  169.                 break;
  170.             case 1:  /* Prev */
  171.                 prevFgPen(drawwin);
  172.                 break;
  173.             case 3:  /* Reset (item 2 is the bar!) */
  174.                 resetFgPen(drawwin);
  175.                 break;
  176.             }
  177.             break;
  178.         case 2:  /* Tools menu */
  179.             switch(itemNumber)
  180.             {
  181.             case 0:  /* Screen Bar */
  182.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  183.                 break;
  184.             case 1:  /* Tool Bar */
  185.                 /* Do the open or close */
  186.                 if(item->Flags & CHECKED)
  187.                 {
  188.                     /* If the open fails, stop immediately */
  189.                     if(!openToolWin())
  190.                         return FALSE;
  191.                 }
  192.                 else
  193.                     closeToolWin();
  194.                 break;
  195.             }
  196.         }
  197.     }
  198.     /* Keep going */
  199.     return TRUE;
  200. }
  201.